package com.egoo.ticket.server.utils.activiti;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;

/**
 * @author：victor_tang
 * @Date ：2020/2/3
 * @Description：流程引擎任务处理工具类
 */
public class ActTaskServiceUtil {
	/**
	 * 获取流程引擎对象的任务处理接口
	 */

	public static TaskService taskService = ProcessEngines.getDefaultProcessEngine().getTaskService();

	/**
	 * TODO 获取当前指定处理人的任务id
	 * @author: victor_tang
	 * @createtime: 2020/4/17 13:34
	 * @param processInstId 流程实例id
	 * @param createUserId 创建人id
	 * @return: java.lang.String
	 */
	public static String getAssignerTaskId(String processInstId, String createUserId){
		ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
		TaskService taskService = processEngine.getTaskService();
		Task task = taskService.createTaskQuery().processInstanceId(processInstId).taskAssignee("张三").singleResult();
		if(null == task){
			task = taskService.createTaskQuery().processInstanceId(processInstId).taskAssignee(createUserId).singleResult();
		}
		String taskId = task.getId();
		return taskId;
	}
	
	/**
	 * TODO 根据流程实例id获取当前正在运行的任务id
	 * @author: victor_tang
	 * @createtime: 2020/4/17 13:35
	 * @param processInstId 流程实例id
	 * @return: java.lang.String
	 */
	public static String getActiveTaskIdByProcessInstId(String processInstId){
		
		Task task = taskService.createTaskQuery().processInstanceId(processInstId).active().singleResult();
		if (task!=null) {
			return task.getId();
		}
		return null;
	}
	
	/**
	 * TODO 根据任务id完成任务
	 * @author: victor_tang
	 * @createtime: 2020/4/17 13:35
	 * @param taskId 任务id
	 * @return: void
	 */
	public static void complateByTaskId(String taskId){
		taskService.complete(taskId);
	}
	
	/**
	 * TODO 修改处理人
	 * @author: victor_tang
	 * @createtime: 2020/4/17 13:35
	 * @param taskId 任务id
	 * @param currentHandlerId 当前处理人id
	 * @return: void
	 */
	public static void updateCurrentHandler(String taskId,String currentHandlerId) {
		taskService.addCandidateUser(taskId, currentHandlerId);
	}
}
